#include "blockqueue.h"

void* ProducerAction(void* arg)
{
  BlockQueue<int>* p = (BlockQueue<int>*)arg;
  while(true)
  {
    int data = rand()%100+1;
    p->PushData(data);
    cout<<"[producer] push data："<<data<<endl;
    sleep(1);
  }
}

void* ConsumerAction(void* arg)
{
  BlockQueue<int>* p = (BlockQueue<int>*)arg;
  while(true)
  {
    int data = 0;
    p->PopData(data);
    cout<<"[consumer] get data："<<data<<endl;
    sleep(1);
  }
}

int main()
{
  srand((unsigned int)time(nullptr));
  // 1、new一个阻塞队列
  BlockQueue<int>* p = new BlockQueue<int>;
  // 2、创建两个新线程，分别代表生产者和消费者
  pthread_t pro, con;
  pthread_create(&pro, nullptr, ProducerAction, p);
  pthread_create(&con, nullptr, ConsumerAction, p);
  // 3、主线程等待它们完成任务后负责销毁阻塞队列
  pthread_join(pro, nullptr);
  pthread_join(pro, nullptr);
  delete p;
  return 0;
}
